package io.noties.markwon.base.image


import android.graphics.Canvas

import timber.log.Timber

import android.graphics.Rect
import android.graphics.drawable.Drawable
import io.noties.markwon.base.image.load.AsyncRecycleDrawableLoader
import io.noties.markwon.base.image.size.ImageSizeResolver

enum class AsyncRecycelDrawableState {
    PRO_LOAD, LOAD, RECYCLE, UN_SET
}

class AsyncRecycleDrawable(
    destination: String,
    private val asyncLoader: AsyncRecycleDrawableLoader,
    imageSizeResolver: ImageSizeResolver,
    imageSize: ImageSize?,
    isBlock: Boolean = true
) : AsyncDrawable(destination, asyncLoader, imageSizeResolver, imageSize,isBlock) {
    //private var preholder: Drawable? = null
    private var preholderRect = Rect()
    private var loadState = AsyncRecycelDrawableState.UN_SET

    fun preLoad(placeholder: Drawable?) {
        this.placeholder = placeholder
        //super.setPlaceholderResult(placeholder);
    }

    fun preLoad(resultBounds: Rect) {
        preholderRect = resultBounds
        placeholder?.let {
            preLoad(it, resultBounds)
        }
    }

    fun preLoad(plaholder: Drawable?, resultBounds: Rect) {
        this.placeholder = plaholder
        preholderRect = resultBounds
        Timber.d("AsyncRecycleDrawableLoader  preLoad palaceHolder is ${placeholder!=null} drawableState:${loadState}, rect:${preholderRect}")
        if ( loadState == AsyncRecycelDrawableState.UN_SET ) {
            loadState = AsyncRecycelDrawableState.PRO_LOAD
            placeholder?.let {
                it.bounds=resultBounds
                setResult(it)
            }
        }
    }

    fun recycle() {
        if (loadState == AsyncRecycelDrawableState.LOAD) {
            loadState = AsyncRecycelDrawableState.RECYCLE
            resultDrawable?.let {
                preholderRect = it.copyBounds()
            }
            placeholder?.let {
                it.bounds = preholderRect
                setResult(it)
            }
            asyncLoader.recycle(this)
        }
    }

    fun preLoad() {
        Timber.d("AsyncRecycleDrawableLoader  preLoad() drawableState:${loadState}, rect:${preholderRect}")
        if (loadState == AsyncRecycelDrawableState.UN_SET)
            asyncLoader.preLoad(this)
    }

    fun reLoad() {
        // @since 4.5.0 we trigger loading only if we have no result (and result is not placeholder)
        if (loadState != AsyncRecycelDrawableState.LOAD) {
            loadState = AsyncRecycelDrawableState.LOAD
            asyncLoader.load2(this)
        }
    }

    /*override fun setResult(result: Drawable) {
        Timber.d("AsyncRecycleDrawableLoader setResult drawableState:${loadState},old rect:${preholderRect},new rect:${result.bounds}")
        super.setResult(result)
    }*/

    override fun draw(canvas: Canvas) {
        Timber.d("AsyncRecycleDrawableLoader draw drawableState:${loadState},drawable:${resultDrawable}")
        if (loadState == AsyncRecycelDrawableState.LOAD)
            super.draw(canvas)
    }
}